package leetcode.templates.maths;

/**
 * 矩形，点，线
 */
public class MaxGeometry {

	// 矩形相交
	public boolean isRectangleOverlap(int[] rec1, int[] rec2) {
		int x1 = Math.max(rec1[0], rec2[0]);
		int x2 = Math.min(rec1[2], rec2[2]);
		if (x1 >= x2) {
			return false;
		}
		int y1 = Math.max(rec1[1], rec2[1]);
		int y2 = Math.min(rec1[3], rec2[3]);
		if (y1 >= y2) {
			return false;
		}
		return true;
	}
	// 通过四个点，验证是否是矩形
	public boolean validSquare1(int[] p1, int[] p2, int[] p3, int[] p4) {
		return this.line2line(p1, p2, p3, p4) && this.line2line(p1, p3, p2, p4) && this.line2line(p1, p4, p2, p3);
	}
	
	// 通过四个点，验证是否是正方形
	public boolean validSquare2(int[] p1, int[] p2, int[] p3, int[] p4) {
		return this.line2line(p1, p2, p3, p4) && this.line2line(p1, p3, p2, p4) && this.line2line(p1, p4, p2, p3)
				&& (this.line2line(p1, p2, p1, p3) || this.line2line(p1, p2, p1, p4) || this.line2line(p1, p3, p1, p4));
	}

	private boolean line2line(int[] p1, int[] p2, int[] p3, int[] p4) {
		long dist1 = line(p1, p2);
		long dist2 = line(p3, p4);
		return dist1 == 0 || dist2 == 0 ? false : dist1 == dist2;
	}

	private long line(int[] p1, int[] p2) {
		long dist1 = Math.abs(p1[0] - p2[0]);
		long dist2 = Math.abs(p1[1] - p2[1]);
		return dist1 * dist1 + dist2 * dist2;
	}

}
